const  getWaiting = (data,type)=> {
    let time = Number(data);
    let wartime = '';
    // if(time/3600>=24){
    //     wartime = "24小时以上";
    //     return wartime;
    // }
    if(type==true){
        let minutes = Math.floor(time/60);
        let seconds = time % 3600 % 60;
        if (minutes < 10) {
            minutes = "0" + minutes;
        }
        if (seconds < 10) {
            seconds = "0" + seconds;
        }
        wartime =  minutes + ':' + seconds;
    }else{
        let hours = Math.floor(time / 3600);
        let minutes = Math.floor(time % 3600 / 60);
        let seconds = time % 3600 % 60;
        if (hours < 10) {
            hours = "0" + hours;
        }
        if (minutes < 10) {
            minutes = "0" + minutes;
        }
        if (seconds < 10) {
            seconds = "0" + seconds;
        }
        wartime = hours + ':' + minutes + ':' + seconds;
    }
    return wartime;
}
const ajax =  function (url,config,success,err) {
    let opt ={
        url: url,
        data: config.data || "",
        method: !config.method ? 'GET' : config.method.toUpperCase(),
        header: config.header || {},
        dataType: config.datatype || 'json',
        complete: function (res) {
            console.log(res)
            if (res.statusCode >= '200' && res.statusCode <= '206' || res.statusCode == '304') {
                if (typeof success =="function" ){
                    success(res.data);
                }
            } else {
                if(typeof err =="function"){
                    err(res);
                }  
            }
        }
    }
    wx.request(opt)
}
const errorToast = function(title){
    wx.hideToast();
    wx.showToast({
        title: title,
        icon:'none'
    })
}
const empty = (str)=>{
    if(str===""||str==undefined||str==null){
        return false;
    }else{
        return true;
    }
}
const isPhone = (str)=>{
    let reg = /^1[3,5,7,8]\d{9}$/;
    return reg.test(str);
}
const sendUserMsg = (data)=>{
    ajax('https://duozs.duocaitou.com/api/members',{
        header:{
            "Accept":"application/vnd.dct.v1+json"
        },
        method:'post',
        data:{
            uid: data.uid,	//string	是	用户id		122
            nickname: data.nickname,	//string	是	昵称		昵称
            avatar: data.avatar,	//string	是	头像		http://www.avatar.com/avatar.png
            account: data.account,	//string	是	多彩投账号		23132@qq.com
            wx_id: data.wx_id,	//string	是	openid		231Weqwas$w3sfs@1asd
            wx_unionid: data.wx_unionid,//	string	是	unionid		34343wewqdf$2#sdvRT341
            accid: data.accid,//	string	是	网易云信accid		wudh4759dhvie72h2k9ddn38
            token: data.token//	string	是	网易云信token
        }
    })
}
const setCodeBtn = (n,t,p)=>{
    let time = wx.getStorageSync(n);
    if (empty(time) == false) {
        time = t;
    }
    p.setData({
        cbdis: true,
        codewait: time,
        btnstate: time + 's后重发'
    })
    let codeTimer = setInterval(() => {
        let time = wx.getStorageSync(n);
        if (empty(time) == false) {
            time = t;
        }
        time--;
        wx.setStorageSync(n, time);
        p.setData({
            codewait: time,
            btnstate: time + 's后重发'
        })
        if (time <= 0) {
            p.setData({
                cbdis: false,
                btnstate: '获取验证码'
            })
            wx.removeStorageSync(n);
            clearInterval(codeTimer)
        }
    }, 1000)
}
const showMsgDate = function(time){
    let msgTime = new Date(time);
    let nowTime = new Date();
    let year = "",month="",date="",hour = "",minutes = "";
    year = msgTime.getFullYear() == nowTime.getFullYear() ? "" : msgTime.getFullYear()+" ";
    month = msgTime.getMonth() + 1;
    date = msgTime.getDate() >= 10 ? msgTime.getDate() : "0" + msgTime.getDate();
    hour = msgTime.getHours() >= 10 ? msgTime.getHours() : "0" + msgTime.getHours();
    minutes = msgTime.getMinutes() >= 10 ? msgTime.getMinutes() : "0" + msgTime.getMinutes();
    let str = year + month + "-" + date + " " + hour + ":" + minutes;
    return str;
}
module.exports = {
    getWaiting: getWaiting,
    ajax:ajax,
    errorToast:errorToast,
    empty:empty,
    isPhone:isPhone,
    setCodeBtn:setCodeBtn,
    sendUserMsg:sendUserMsg,
    showMsgDate:showMsgDate
}
